package com.jeequan.jeepay.core.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Data
@Slf4j
public class LogUtils {

//    static ValueFilter valueFilter = pro

    protected static ObjectMapper mapper = new ObjectMapper();

    private String operation;

    private Map<String, Object> params = new HashMap<>();

    private String message = "";

    private boolean catReport = false;

    private static final String LOG_FORMAT = "%s|%s|%s|%s";

    private static final String TRACEID = "traceId";

    public LogUtils(String operation) {
        this.operation = operation;
    }

    public static LogUtils op(String op) {
        return new LogUtils(op);
    }

    public LogUtils kv(String key, final Object value) {
        String valueStr = JSON.toJSONString(value);
        this.params.put(key, valueStr);
        return this;
    }

    public LogUtils notMask(String key, final Object value) {
        String valueStr = JSON.toJSONString(value);
        this.params.put(key, valueStr);
        return this;
    }

    public LogUtils msg(String message, Object... args) {
        this.message = String.format(message, args);
        return this;
    }

    public LogUtils catReport(boolean flag) {
        this.catReport = flag;
        return this;
    }

    public String toString() {
        String result = String.format(LOG_FORMAT, this.operation, this.message, this.params.toString(), this.catReport);
        return result;
    }

    public void info() {
        log.info(this.toString());
    }

    public void warn() {
        log.warn(this.toString());
    }

    public void error() {
        log.error(this.toString());
    }

}
